<?php

function updateSrv(){
    
    global $prefix;
    global $lang;
    $fields = array(
        'ip' => '',
        'name' => ''
    );
    $id = $_POST['id'];
    $name = $_POST['name'];
    $ip = $_POST['ip'];
    
    $query = 'SELECT id FROM '.$prefix.'_SRV WHERE name = \''.mysql_escape_string($name).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            $srv = mysql_fetch_array($result);
            if($srv['id']!=$id){
                echo $lang['NAME_TAKEN'];
                echo '<br/>';
                return false;
            }
        }
    }
    $query = 'SELECT id FROM '.$prefix.'_SRV WHERE ip = \''.mysql_escape_string($ip).'\'';
    $result = mysql_query($query);
     if($result){
        if(mysql_num_rows($result)>0){
            $srv = mysql_fetch_array($result);
            if($srv['id']!=$id){
                echo $lang['IP_TAKEN'];
                echo '<br/>';
                return false;
            }
        }
    }
    $query = createUpdateStatement($fields, $prefix.'_SRV', true, 'id', $id);
    $result = mysql_query($query);
    if(!$result){
        $error = mysql_error();
        echo $error;
		echo '<br/>';
        return false;
    } 
    return true;
}

function createNewSrv(){
    global $prefix;
    global $lang;
   
    $name='';
    if(isset($_POST['name']))
        $name = $_POST['name'];
    if($name == ''){
        echo $lang['NAME_REQ'];
        echo '<br/>';
        return;
    }
    $ip='';

    if(isset($_POST['ip']))
        $ip = trim($_POST['ip']);
    
    if($ip==''){
        echo $lang['IP_REQ'];
        echo '<br/>';
        return;
    }  
     
    $fields = array(
        'ip' => '',
        'name' => ''
    );
    $query = 'SELECT id FROM '.$prefix.'_SRV WHERE name = \''.mysql_escape_string($name).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            echo $lang['NAME_TAKEN'];
            echo '<br/>';
            return;
        }
    }
    $query = 'SELECT 1 FROM '.$prefix.'_SRV WHERE ip = \''.mysql_escape_string($ip).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            echo $lang['IP_TAKEN'];
            echo '<br/>';
            return;
        }
    }
    $query = createInsertStatement($fields, $prefix.'_SRV', true);
    $result = mysql_query($query);
    if(!$result){
        echo mysql_error();
		echo '<br/>';
    } 
}
function deleteSrv(){
    global $prefix;
    
    $query = 'DELETE FROM '.$prefix.'_SRV WHERE id = '.$_POST['id'];
    $result = mysql_query($query);
	if(!$result){
        echo mysql_error();
        echo '<br/>';		
    }
}

function showSrvDetails($id, $isEdit){
    global $lang;
    global $prefix;
    $isNew = false;
    if($id != null){
        $query = 'SELECT * FROM '.$prefix.'_SRV WHERE id = '.$id;
    	$result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
        } else {
            $record = mysql_fetch_array($result);
        }
    }
    else {
        $record = null;
        $isNew = true;
    }
	echo '         <form class="fill" name="form_srv" method="POST" action="main.php?action=srv">
                      <input name="id" type="hidden" value="'.$record['id'].'"/>
                      <table class="tab_details fill">';
    if(($isNew && hasAccess('add_srv')) || hasAccess('edit_srv')){
        echo '          <tr class="half"><td class="fill25"/>';
        addText('NAME', 'name', $record, '', null, false);
        echo '          </tr>
                        <tr><td class="fill25"/>';
        addText('IP', 'ip', $record, '', null, false);        
        echo '          </tr>';
    } else {
        echo '          <tr><td class="fill25"/>
                            <th class="right fill25">'.$lang['NAME'].'</th>
                            <td class="readonly_value">'.$record['name'].'</td>
                        </tr>
                        <tr><td class="fill25"/>
                            <th class="right fill25">'.$lang['IP'].'</th>
                            <td class="readonly_value">'.$record['ip'].'</td>
                        </tr>';
    }
    if($isNew || hasAccess('edit_srv'))
        echo '<tr><td colspan="3" class="right">';
    if($isNew) {
        echo '<input class="btn" type="submit" name="ins" value="'.$lang['B_SAVE'].'"/>';
    } else if(hasAccess('edit_srv')) {
        echo '<input class="btn"  type="button" name="edit" value="'.$lang['B_EDIT'].'" onclick="toogleInputs();"/>'; 
        echo '<input class="btn save"  type="submit" disabled="true" name="upd" id="upd" value="'.$lang['B_SAVE'].'"/>';
    } 
    if(!$isNew && hasAccess('del_srv')){
        echo '<input class="btn" type="submit" name="del" value="'.$lang['B_DELETE'].'" onclick="return confirmDelete()"/>';
    }
    if($isNew || hasAccess('edit_srv'))
        echo '            </td></tr>';
    echo '</table></form>';
    
    if($isEdit || $isNew){
        echo '<script type="text/javascript">
                enableInputs(true);
        </script>';
    } 
}
?>
